package com.renwei.yunlong.activity.contacts;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.CompanyCertificateListAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CertificateInfoBean;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.event.MinePageRefreshEvent;
import com.renwei.yunlong.global.EventKey;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.BasePopwindow;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 证书--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class CompanyCertificateActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener, XRecyclerView.LoadingListener, CompanyCertificateListAdapter.onCertificateClickListener, StateLayout.OnViewRefreshListener {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_right)
    ImageView ivRight;
    @Bind(R.id.rlv_certificate)
    XRecyclerView rlvCertificate;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private String TAG = "CompanyCertificateActivity";
    private List<CertificateInfoBean.RowsBean> certificateList;
    private CompanyCertificateListAdapter adapter;
    private String from = "info";//判断证书列表是从信息页面跳转过来的还是从编辑信息页面调转过来的
    private String certificateType = "32";//证书类型，证书类型为32代表要查看的是企业资质证书，31代表的是普通证书
    private String title = "证件";
    private String friendCompanyCode;
    private BasePopwindow popupWindow;
    private HashMap paramMap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_certificate);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        certificateType = TextUtils.isEmpty(getIntent().getStringExtra("certificateType")) ? "32" : getIntent().getStringExtra("certificateType");
        from = TextUtils.isEmpty(getIntent().getStringExtra("from")) ? "info" : getIntent().getStringExtra("from");
        friendCompanyCode = TextUtils.isEmpty(getIntent().getStringExtra("friendCompanyCode")) ? "" : getIntent().getStringExtra("friendCompanyCode");
        title = "".equals(StringUtils.value(getIntent().getStringExtra("title"))) ?
                "证书" : StringUtils.value(getIntent().getStringExtra("title"));
        initView();
    }

    private void initView() {
        if ("info".equals(from) || "friend".equals(from)) {
            ivRight.setVisibility(View.GONE);
        } else {
            ivRight.setVisibility(View.VISIBLE);
        }
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText(title);
        ivBack.setOnClickListener(this);
        ivRight.setOnClickListener(this);
        initData();
    }

    private void initData() {
        HashMap queryMap = new HashMap();
        if ("info".equals(from) || "else".equals(from)) {
            if ("1".equals(companyType)) {
                queryMap.put("companyCode", ownerBean.getRows().getCompany().getCompanyCode());
                queryMap.put("type", certificateType);
            } else {
                queryMap.put("companyCode", serviceLoginBean.getRows().getCompany().getCompanyCode());
                queryMap.put("type", certificateType);
            }
        } else {
            queryMap.put("companyCode", friendCompanyCode);
            queryMap.put("type", certificateType);
        }
        ServiceRequestManager.getManager().QueryCompanyCertificate(this, JsonMapUtil.mapToJson(queryMap), this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvCertificate.setLayoutManager(layoutManager);
        rlvCertificate.setLoadingListener(this);
        adapter = new CompanyCertificateListAdapter(this);
        adapter.setOnCertificateListener(this);
        stateLayout.setRefreshListener(this);
        rlvCertificate.setAdapter(adapter);
        popupWindow = new BasePopwindow(this, "");
        View popView = LayoutInflater.from(this).inflate(R.layout.popu_certificate_layout, null, false);
        popView.findViewById(R.id.pop_delete).setOnClickListener(this);
        paramMap = new HashMap();
        popupWindow.initUI(popView);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_right:
                Intent intent = new Intent(this, CompanyAddCertificateActivity.class);
                intent.putExtra("certificateType", certificateType);
                startActivity(intent);
                break;
            case R.id.pop_delete:
                ServiceRequestManager.getManager().DeleteCompanyCertificate(this, JsonMapUtil.mapToJson(paramMap), this);
                break;
            /*case R.id.pop_edit:
                showCenterInfoMsg("编辑");
                break;*/
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(MinePageRefreshEvent event) {
        switch (event.getMessage()) {
            case EventKey.UPDATE_COMPANY_CERTIFICATE:
                initData();
                break;
            default:
                break;
        }
    }


    @Override
    public void onSuccess(int requestId, String result) {
        LogUtil.e(TAG + "回掉接口:---requestId:" + requestId + "\n------------result" + result);
        switch (requestId) {
            case 880:
                CertificateInfoBean certificateBean = new Gson().fromJson(result, CertificateInfoBean.class);
                certificateList = certificateBean.getRows();
                if (certificateList == null || certificateList.size() == 0) {
                    stateLayout.showEmptyView();
                } else {
                    stateLayout.showContentView();
                    adapter.refresh(certificateList, from);
                }
                break;
            case 878:
                CommonBean deleteBean = new Gson().fromJson(result, CommonBean.class);
                if (deleteBean.getMessage().getCode() == 200) {
                    popupWindow.dismiss();
                    showCenterInfoMsg("删除成功");
                } else {
                    showCenterInfoMsg("删除失败");
                }
                onRefresh();
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
        LogUtil.e(TAG + "回掉接口:---requestId:" + requestId + "\n------------code" + code);
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
        stateLayout.showErrorView();
    }

    @Override
    public void onRefresh() {
        initData();
        if (rlvCertificate!=null)
        {
            rlvCertificate.loadMoreComplete();
            rlvCertificate.refreshComplete();
        }
    }

    @Override
    public void onLoadMore() {
       if (rlvCertificate!=null)
       {
           rlvCertificate.loadMoreComplete();
           rlvCertificate.refreshComplete();
       }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void clickEdit() {

    }

    @Override
    public void showPop(View view, String id, String attachPath) {
        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int x = (80 - popupWindow.getContentView().getMeasuredWidth());
        popupWindow.showAsDropDown(view, x, 0 - view.getMeasuredHeight() / 3);
        paramMap.put("attachPath", attachPath);
        paramMap.put("id", id);
    }

    @Override
    public void refreshClick() {
        onRefresh();
    }

    @Override
    public void loginClick() {

    }
}
